package com.orangelife.mobile.individual.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.common.jump.LocationJumper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.zoning.adapter.SelectCommunityListViewAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by YangWei
 * on 2015/10/10.
 */
@Deprecated
public class MyCommunityActivity extends OrangeLifeBaseActivity {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_one)
    TextView tvOne;
    @Bind(R.id.tvCurrentComm)
    TextView tvCurrentComm;
    @Bind(R.id.tvCleanRecord)
    TextView tvCleanRecord;
    @Bind(R.id.lvHistoryComm)
    ListView lvHistoryComm;

    SelectCommunityListViewAdapter<Map<String, Object>> adapter;
    private List<Map<String, Object>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_community);
        ButterKnife.bind(this);
        initView();
        findView();
    }

    /**
     * 初始化
     */
    private void initView() {
        list = new LinkedList<>();
    }

    /**
     * 加载数据等
     */
    private void findView() {
        tvTitle.setText("我的小区");
        tvOne.setText("更多小区");
        tvCurrentComm.setText("");

        list = getHistoryCommList();
        adapter = new SelectCommunityListViewAdapter<>(this, list);
        lvHistoryComm.setAdapter(adapter);

        tvCleanRecord.setOnClickListener(listener);
        tvOne.setOnClickListener(listener);

        lvHistoryComm.setOnItemClickListener(itemListener);
        lvHistoryComm.setOnItemLongClickListener(longClickListener);

    }

    /**
     * 获取历史小区数据
     *
     * @return
     */
    private List<Map<String, Object>> getHistoryCommList() {
        List<Map<String, Object>> listHist = new ArrayList<>();
        Set<String> set = new HashSet<>();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            String str = (String) it.next();
            try {
                JSONObject jo = new JSONObject(str);
                Map<String, Object> map = new HashMap<>();
                map.put("num", String.valueOf(jo.get("num")));
                map.put("city", String.valueOf(jo.get("city")));
                map.put("commID", String.valueOf(jo.get("commID")));
                map.put("comm", String.valueOf(jo.get("comm")));
                map.put("stree", String.valueOf(jo.get("stree")));
                map.put("distance", String.valueOf(jo.get("distance")));
                map.put("commNamePyAll", String.valueOf(jo.get("commNamePyAll")));
                map.put("districtID", String.valueOf(jo.get("districtID")));
                map.put("phone", String.valueOf(jo.get("phone")));
                listHist.add(map);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        List<Map<String, Object>> listOrder = new LinkedList<>();
        for (int i = listHist.size() - 1; i >= 0; i--) {
            for (int j = 0; j < listHist.size(); j++) {
                String iStr = i + 1 + "";
                if (iStr.equals(String.valueOf(listHist.get(j).get("num")))) {
                    listOrder.add(listHist.get(j));
                }
            }
        }

        return listOrder;
    }

    /**
     * 设置监听器
     */
    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tvCleanRecord:
                    delAllDialog();
                    break;
                case R.id.tv_one:
                    jump();
                    break;
                default:
                    break;
            }
        }
    };

    public void jump() {
        LocationJumper.jumpToSelectCity(this);
    }

    /**
     * 点击列表
     */
    private AdapterView.OnItemClickListener itemListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

            /* 删除小区历史记录中的当前选中的小区信息 */
            Set<String> commSet = new HashSet<String>();
            HashMap<String, Object> mapFormer = new HashMap<>();
            List<Map<String, Object>> listTemp = new ArrayList<>();
            listTemp = getHistoryCommList();
            String currComm = String.valueOf(listTemp.get(position).get("comm"));
            int num = 0, num1 = 0;
            try {
                /* 遍历历史小区记录，删除当前选中小区在历史小区记录中的信息，并记录该小区是第几个添加进历史记录的小区 */
                Set<String> setTemp = new HashSet<>();
                Iterator it = commSet.iterator();
                while (it.hasNext()) {
                    String temp = (String) it.next();
                    JSONObject jo = new JSONObject(temp);
                    if (!currComm.equals(jo.get("comm"))) {
                        setTemp.add(jo.toString());
                    } else {
                        num = Integer.parseInt(String.valueOf(jo.get("num")));
                    }

                }

                Set<String> setTemp1 = new HashSet<>();
                Iterator it1 = setTemp.iterator();
                /* 遍历历史记录，把在被删除小区之后添加进来的小区往前移动一位 */
                while (it1.hasNext()) {
                    String temp1 = (String) it1.next();
                    JSONObject jo1 = new JSONObject(temp1);
                    num1 = Integer.parseInt(String.valueOf(jo1.get("num")));

                    if (num1 > num) {
                        num1 = num1 - 1;
                        jo1.put("num", num1 + "");
                    }
                    setTemp1.add(jo1.toString());
                }

                JSONObject jo2 = new JSONObject();
                jo2.put("num", (setTemp1.size() + 1) + "");
                jo2.put("city", String.valueOf(mapFormer.get("city")));
                jo2.put("commID", String.valueOf(mapFormer.get("commID")));
                jo2.put("comm", String.valueOf(mapFormer.get("comm")));
                jo2.put("distance", String.valueOf(mapFormer.get("distance")));
                jo2.put("stree", String.valueOf(mapFormer.get("stree")));
                jo2.put("commNamePyAll", String.valueOf(mapFormer.get("commNamePyAll")));
                jo2.put("districtID", String.valueOf(mapFormer.get("districtID")));
                jo2.put("phone", String.valueOf(mapFormer.get("phone")));
                setTemp1.add(jo2.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }

            HashMap<String, Object> map = new HashMap<>();
            map.put("city", String.valueOf(listTemp.get(position).get("city")));
            map.put("commID", String.valueOf(listTemp.get(position).get("commID")));
            map.put("comm", String.valueOf(listTemp.get(position).get("comm")));
            map.put("distance", String.valueOf(listTemp.get(position).get("distance")));
            map.put("stree", String.valueOf(listTemp.get(position).get("stree")));
            map.put("commNamePyAll", String.valueOf(listTemp.get(position).get("commNamePyAll")));
            map.put("districtID", String.valueOf(listTemp.get(position).get("districtID")));
            map.put("phone", String.valueOf(listTemp.get(position).get("phone")));
            tvCurrentComm.setText(String.valueOf(listTemp.get(position).get("comm")));

            List<Map<String, Object>> listComm = new LinkedList<>();
            listComm = getHistoryCommList();
            adapter.setList(listComm);
            adapter.notifyDataSetChanged();

        }
    };

    /**
     * 长按删除
     */
    private AdapterView.OnItemLongClickListener longClickListener = new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
            delDialog(position);
            return false;
        }
    };

    /**
     * 长按删除对话框
     *
     * @param position
     */
    private void delDialog(final int position) {

        final ConfirmDialog dialog = new ConfirmDialog(this, "温馨提示", "确认该条记录？", "取消", "确认");
        dialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doLeft() {
                dialog.dismiss();
            }

            @Override
            public void doRight() {
                List<Map<String, Object>> listDel = new ArrayList<Map<String, Object>>();
                List<Map<String, Object>> listDel1 = new ArrayList<Map<String, Object>>();
                Set<String> setDel = new HashSet<String>();
                Set<String> setTemp = new HashSet<String>();
                listDel = getHistoryCommList();
                int num = Integer.parseInt(String.valueOf(listDel.get(position).get("num")));

                Iterator it = setDel.iterator();
                while (it.hasNext()) {
                    String strDel = (String) it.next();
                    try {
                        JSONObject jo = new JSONObject(strDel);
                        int num1 = Integer.parseInt(String.valueOf(jo.get("num")));
                        if (num1 > num) {
                            num1 = num1 - 1;
                            jo.put("num", num1 + "");
                            setTemp.add(jo.toString());
                        } else if (num1 < num) {
                            setTemp.add(jo.toString());
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                listDel1 = getHistoryCommList();
                adapter.setList(listDel1);
                adapter.notifyDataSetChanged();
                dialog.dismiss();
            }
        });

        dialog.show();
    }
    private void delDialog1(final int position) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确认该条记录？");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new android.content.DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                List<Map<String, Object>> listDel = new ArrayList<Map<String, Object>>();
                List<Map<String, Object>> listDel1 = new ArrayList<Map<String, Object>>();
                Set<String> setDel = new HashSet<String>();
                Set<String> setTemp = new HashSet<String>();
                listDel = getHistoryCommList();
                int num = Integer.parseInt(String.valueOf(listDel.get(position).get("num")));

                Iterator it = setDel.iterator();
                while (it.hasNext()) {
                    String strDel = (String) it.next();
                    try {
                        JSONObject jo = new JSONObject(strDel);
                        int num1 = Integer.parseInt(String.valueOf(jo.get("num")));
                        if (num1 > num) {
                            num1 = num1 - 1;
                            jo.put("num", num1 + "");
                            setTemp.add(jo.toString());
                        } else if (num1 < num) {
                            setTemp.add(jo.toString());
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                listDel1 = getHistoryCommList();
                adapter.setList(listDel1);
                adapter.notifyDataSetChanged();
            }
        });
        builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }

        });
        builder.create().show();
    }


    private void delAllDialog() {

        final ConfirmDialog dialog = new ConfirmDialog(this, "温馨提示", "确认清除全部记录？", "取消", "确认");
        dialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doLeft() {
                dialog.dismiss();
            }

            @Override
            public void doRight() {
                list.clear();
                adapter.setList(list);
                adapter.notifyDataSetChanged();
            }
        });

        dialog.show();
    }

    private void delAllDialog1() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确认清除全部记录？");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new android.content.DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                list.clear();
                adapter.setList(list);
                adapter.notifyDataSetChanged();
            }
        });
        builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }

        });
        builder.create().show();
    }


}
